package com.vk.vkapiinterface.controller;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.URLUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @ClassName : FunApiController
 * @Description : 有意思的接口控制器类
 * @Author : LYQ
 * @Date: 2023-12-04 18:51
 */
@RestController
public class FunApiController {

    /**
     * 调用外部API
     *
     * @param url   请求的API
     * @param body  body请求体
     * @return 响应体
     */
    private static String invokeOuterApi(String url, String body) {
        try(HttpResponse httpResponse = HttpRequest.get(url + "?" + body)
                .execute()){
            return httpResponse.body();
        }
    }

    /**
     * 随机头像
     */
    @PostMapping("/rand.avatar")
    public String randAvatar(HttpServletRequest request) {
        String url = "https://api.uomg.com/api/rand.avatar";
        String body = URLUtil.decode(request.getHeader("body"), CharsetUtil.CHARSET_UTF_8);
        return invokeOuterApi(url, body);
    }

    /**
     * 随机壁纸
     */
    @PostMapping("/sjbz")
    public String randImages(HttpServletRequest request) {
        String url = "http://api.btstu.cn/sjbz/api.php";
        String body = URLUtil.decode(request.getHeader("body"), CharsetUtil.CHARSET_UTF_8);
        return invokeOuterApi(url, body);
    }

    /**
     * 毒鸡汤
     */
    @PostMapping("/poison")
    public String poisonChicken(HttpServletRequest request) {
        String url = "http://api.btstu.cn/yan/api.php";
        String body = URLUtil.decode(request.getHeader("body"), CharsetUtil.CHARSET_UTF_8);
        return invokeOuterApi(url, body);
    }

    /**
     * 短网址生成
     */
    @PostMapping("/long2dwz")
    public String long2dwz(HttpServletRequest request) {
        String url = "https://api.uomg.com/api/long2dwz";
        String body = URLUtil.decode(request.getHeader("body"), CharsetUtil.CHARSET_UTF_8);
        return invokeOuterApi(url, body);
    }

    /**
     * 获取qq头像和名字，没有效果
     */
    @PostMapping("/QQname")
    public String QQname(HttpServletRequest request) {
        String url = "http://api.btstu.cn/qqxt/api.php";
        String body = URLUtil.decode(request.getHeader("body"), CharsetUtil.CHARSET_UTF_8);
        return invokeOuterApi(url, body);
    }

    /**
     * 笑话大全
     */
    @PostMapping("/joke")
    public String randJoke(HttpServletRequest request) {
        String url = "http://v.juhe.cn/joke/content/list.php";
        String body = URLUtil.decode(request.getHeader("body"), CharsetUtil.CHARSET_UTF_8);
        return invokeOuterApi(url, body);
    }

    /**
     * 新闻头条
     *
     * @param request
     * @return
     */
    @PostMapping("/headlineNews")
    public String headlineNews(HttpServletRequest request) {
        String url = "http://v.juhe.cn/toutiao/index";
        String body = URLUtil.decode(request.getHeader("body"), CharsetUtil.CHARSET_UTF_8);
        return invokeOuterApi(url, body);
    }

    /**
     * 汇率
     *
     * @param request
     * @return
     */
    @PostMapping("/exchangeRate")
    public String exchangeRate(HttpServletRequest request) {
        String url = "http://op.juhe.cn/onebox/exchange/list";
        String body = URLUtil.decode(request.getHeader("body"), CharsetUtil.CHARSET_UTF_8);
        return invokeOuterApi(url, body);
    }
}
